package com.buddies.lottery.ui.user;

import com.buddies.lottery.base.BasePresenter;
import com.buddies.lottery.common.ConfigManager;
import com.buddies.lottery.data.BaseObserver;
import com.buddies.lottery.data.DataManager;
import com.buddies.lottery.data.bean.User;
import com.buddies.lottery.data.bean.Wallet;
import com.buddies.lottery.data.resp.BalanceResponse;
import com.buddies.lottery.data.resp.CheckWalletPwdResponse;

/**
 * Created by xuliang on 2018/2/28.
 */

public class UserPresenter extends BasePresenter<UserContract.View> implements UserContract.Presenter {

    public UserPresenter(UserContract.View view) {
        super(view);
    }

    @Override
    public void loadBalance() {
        addSubscription(DataManager.getInstance().getUserRepo().getBalance(), new BaseObserver<BalanceResponse>() {
            @Override
            public void onNext(BalanceResponse response) {
                ConfigManager.getInstance().setBalance(response.balance);
                mView.onUpdateBalance();
            }

            @Override
            public void onError(Throwable e) {
                mView.onError(e);
            }
        });
    }

    @Override
    public void loadUserInfo(long userId) {
        addSubscription(DataManager.getInstance().getUserRepo().getUserInfo(userId), new BaseObserver<User>() {
            @Override
            public void onNext(User user) {
                ConfigManager.getInstance().setUser(user);
            }

            @Override
            public void onError(Throwable e) {
                mView.onError(e);
            }
        });
    }

    @Override
    public void loadWallet() {
        addSubscription(DataManager.getInstance().getUserRepo().getWallet(), new BaseObserver<Wallet>() {
            @Override
            public void onNext(Wallet response) {
                ConfigManager.getInstance().setWallet(response);
                mView.onUpdateWallet();
            }

            @Override
            public void onError(Throwable e) {
                mView.onError(e);
            }
        });
    }

    @Override
    public void checkWalletPwd(String pwd) {
        addSubscription(DataManager.getInstance().getUserRepo().checkWalletPwd(pwd), new BaseObserver<CheckWalletPwdResponse>() {
            @Override
            public void onNext(CheckWalletPwdResponse response) {
                mView.onCheckWalletPwdSuccess(response);
            }

            @Override
            public void onError(Throwable e) {
                mView.onError(e);
            }
        });
    }

}
